<!--
 * @Author: 矫建武 1452239416@qq.com
 * @Date: 2024-10-14 17:53:38
 * @LastEditTime: 2024-12-18 14:28:09
 * @FilePath: \tool\src\views\component\toolFunction.vue
 * @Description: 
 * 
 * @file: https://github.com/OBKoro1/koro1FileHeader/wiki/%E5%AE%89%E8%A3%85%E5%92%8C%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8B
-->
<!-- 模块说明 -->
<style lang="scss" scoped>
.demo-collapse {
  position: relative;
}

.collapse-tool {
  width: 100%;
  margin-bottom: 10px;
  position: absolute;
  right: 0;
  text-align: right;
}

.collapse-title {
  display: flex;
  justify-content: space-between;
  width: 100%;
}

.floatSearch {
  width: calc(100% - 285px);
  height: 20px;
  background-color: var(--transparent-background);
  display: flex;
  align-items: center;
  position: fixed;
  top: 20px;
  box-shadow:
    0 0 20px rgba(0, 0, 0, 0.5),
    0 0 40px rgba(0, 0, 0, 0.5),
    0 0 60px rgba(0, 0, 0, 0.5);
}

.searchipt {
  margin-bottom: 5px;
}

// position: relative;
.searchContent {
  position: absolute;
  width: 13.7vw;
  background-color: #fff;
  z-index: 2;
  transition: 0.3s;
  box-shadow: 0 10px 20px rgba(255, 255, 255, 0.5);
  overflow-y: auto;
  box-sizing: border-box;
  border: 1px solid #d4cfcf;
  border-radius: 5px;

  .searchContent-item {
    height: 40px;
    width: 12.5vw;
    border: 1px solid #999;
    border-radius: 5px;
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    box-sizing: border-box;
    cursor: pointer;

    &:hover {
      background-color: #f6f6f6;
    }
  }
}

.floatSearch {
  position: fixed;
  right: 5px;
  top: 58px;
  width: 235px;

  .searchContent {
    position: absolute;
    width: 235px;
    background-color: #fff;
    z-index: 2;
    transition: 0.3s;
    overflow-y: auto;
    box-sizing: border-box;
    top: 27px;
    box-sizing: border-box;
    border: 1px solid #d4cfcf;
    border-radius: 0 0 5px 5px;

    .searchContent-item {
      height: 40px;
      width: 215px;
      border: 1px solid #999;
      border-radius: 5px;
      margin-bottom: 10px;
      display: flex;
      align-items: center;
      padding: 5px;
      box-sizing: border-box;
      cursor: pointer;

      &:hover {
        color: #0966b4;
        background: #e6f2fb;
      }
    }
  }
}
</style>
<template>
  <FloatWindow>
    <div class="__cards__">
      <el-button-group style="width: 100%; margin-bottom: 10px">
        <el-button :type="active === 'all' ? 'primary' : ''" style="width: 50%" @click="handleClick('all')">
          全部
        </el-button>
        <el-button :type="active === 'my' ? 'primary' : ''" style="width: 50%" @click="handleClick('my')">
          我的
        </el-button>
      </el-button-group>

      <el-input placeholder="请输入工具名称" v-model="searchName" @focus="focus_2" @blur="blur_2" />
      <main class="searchContent" :style="{
        height: focusStatus.focus_2 ? '300px' : '0',
        padding: focusStatus.focus_2 ? '10px' : '0'
      }">
        <div class="searchContent-item" v-for="ele in searchList" :key="ele.id" @click="location(ele)">
          {{ ele.title }}
        </div>
      </main>

      <el-button :icon="Plus" style="width: 100%; margin: 10px 0" @click="add">
        加一条
      </el-button>
    </div>
  </FloatWindow>

  <div class="demo-collapse">
    <el-collapse v-model="activeNames">
      <el-collapse-item v-for="(ele, i) in toolData" :key="ele.id" :name="ele.id" :id="'A' + ele.id">
        <template #title>
          <div class="collapse-title">
            <span>&nbsp;&nbsp;&nbsp;&nbsp;{{ i + 1 }} 、{{ ele.title }}</span>
            <span v-if="active === 'all'">更新时间：{{ ele.updateTime || ele.createTime }}&nbsp;</span>
            <div v-else>
              <el-button type="text" :icon="Edit" @click.stop="editFun(ele)">
                编辑
              </el-button>
              <el-button link type="danger" :icon="Delete" @click.stop="delFun(ele)">
                删除
              </el-button>
            </div>
          </div>
        </template>

        <div class="collapse-tool">
          <el-button type="primary" @click="copyText(ele.content)" size="small">
            复制代码
          </el-button>
        </div>
        <Prism language="javascript">
          {{ ele.content }}
        </Prism>
      </el-collapse-item>
    </el-collapse>
  </div>

  <el-dialog v-model="dialogVisible" title="新增函数" width="800">
    <el-form>
      <el-form-item label="名称">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item label="代码">
        <div style="width: 100%; height: 350px">
          <Codemirror :options="{ mode: 'text/javascript' }" border v-model:value="form.content"
            style="width: 700px; height: 350px" />
        </div>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="dialogVisible = false">
        取消
      </el-button>
      <el-button type="primary" @click="onOk">
        确定
      </el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import Prism from 'vue-prism-component'
import 'prismjs/themes/prism-tomorrow.css'
import 'codemirror/mode/javascript/javascript.js'
import Codemirror from 'codemirror-editor-vue3'
import { copyText, fuzzySearch, Message, MessageBox } from '@/utils'
import { onMounted, onUnmounted, ref, reactive, watch, nextTick } from 'vue'
import { getToolFunList, addToolFun, editToolFun, delToolFun } from '@/api'
import { Plus, Delete, Edit } from '@element-plus/icons-vue'
import useStore from '@/utils/store'
const store = useStore()
const form = reactive({
  title: '',
  content: '',
  id: ''
})

const focusStatus = reactive({ focus_2: false })
const searchList = ref([])
const active = ref('all')
const searchName = ref('')
const activeNames = ref([])
const toolData = ref([])
const Main = ref(null)
const floatSearchStatus = ref(false)
const dialogVisible = ref(false)

const floatSearch = (e) => {
  let top = e.target.scrollTop
  if (top > 50) {
    floatSearchStatus.value = true
  } else {
    floatSearchStatus.value = false
  }
}

const handleClick = async (val = 'all') => {
  store.loading = true
  active.value = val
  const res = await getToolFunList(val)
  store.loading = false
  if (res.code === 200) {
    toolData.value = res.data
  }
}

const add = () => {
  dialogVisible.value = true
  form.title = ''
  form.content = `
    /**
     *函数作用  这是一个示例函数
    *@params1 参数含义
    *@params2 参数含义
    */
      function debounce(params1, params2) {
        console.log(params1, params2)
    }
  `
}

const onOk = async () => {
  if (form.title && form.content) {
    let res = null
    const { id, title, content } = form
    if (id) {
      res = await editToolFun({ id, title, content })
    } else {
      res = await addToolFun({ title, content })
    }
    if (res.code === 200) {
      Message.success('操作成功')
      handleClick()
      dialogVisible.value = false
    }
  } else {
    Message.error('请他妈的填写完整')
  }
}

const editFun = (ele) => {
  dialogVisible.value = true
  nextTick(() => {
    form.title = ele.title
    form.content = ele.content
    form.id = ele.id
  })
}
const delFun = (ele) => {
  MessageBox({
    title: '删除',
    message: `确定删除名称为"${ele.title}"的函数吗?`
  }).then(async () => {
    const res = await delToolFun({ id: ele.id })
    console.log(res)
    if (res.code === 200) {
      Message.success('操作成功')
      handleClick()
    }
  })
}
/**定位到对应的方法位置*/
const location = (ele) => {
  const Node = document.querySelector('#A' + ele.id)
  if (Node) {
    activeNames.value = [ele.id]
    setTimeout(() => {
      Node.scrollIntoView({
        behavior: 'smooth',
        block: 'center'
      })
    }, 100)
  }
}

watch(
  () => searchName.value,
  (val) => {
    if (val) {
      searchList.value = fuzzySearch(toolData.value, val, 'title')
    }
  },
  { deep: true }
)

const focus_2 = () => {
  focusStatus.focus_2 = true
  searchList.value = []
}
const blur_2 = () => {
  focusStatus.focus_2 = false
  searchName.value = ''
}

onMounted(() => {
  handleClick()
  Main.value = document.querySelector('.content_main')
  Main.value.addEventListener('scroll', floatSearch)
})
onUnmounted(() => {
  Main.value.removeEventListener('scroll', floatSearch)
  floatSearchStatus.value = false
})
</script>
